import ij.IJ;
import ij.process.ImageProcessor;

import java.awt.Point;
import java.util.List;
import java.util.Random;


public class TrajectoryUtils {

	public static final String PROPKEY = "prWbfSA";	// key to this property

	static int[][][] makeTrajectoryArray(List<Point[]> trajList) {
		int minLength = Integer.MAX_VALUE;
		for (Point[] trajectory : trajList) {
			if (trajectory.length < minLength) {
				minLength = trajectory.length;
			}
		}
		if (minLength < 3)
			return null;
		else {
			int m = trajList.size(); // number of trajectories
			int n = minLength; // length of each trajectory
			int[][][] trajArray = new int[m][n][2];
			int i = 0; // trajectory i
			for (Point[] trajectory : trajList) {
				for (int k = 0; k < minLength; k++) { // point/frame k
					Point p = trajectory[k];
					trajArray[i][k][0] = p.x;
					trajArray[i][k][1] = p.y;
				}
				i = i + 1;
			}
			return trajArray;
		}
	}

	static void printTrajectoryArray(int[][][] trajArray, String name) {
		int m = trajArray.length;
		int n = trajArray[0].length;
		IJ.log("// trajectory array:");
		IJ.log("int[][][] " + name + " = {");
		for (int i = 0; i < m; i++) { // for each trajectory i
			String trajString = "{";
			for (int k = 0; k < n; k++) { // for each point k
				int x = trajArray[i][k][0];
				int y = trajArray[i][k][1];
				if (k > 0)
					trajString += ",";
				trajString += "{" + x + "," + y + "}";
			}
			trajString += "}";
			if (i < m - 1)
				trajString += ",";
			IJ.log(trajString);
		}
		IJ.log("};");
	}

	static void drawSquare(Point p, ImageProcessor ip, int r) {
		ip.drawRect(p.x - r, p.y - r, r + r + 1, r + r + 1);
	}
	
	static void scrambleTrajectoryArray(int[][][] trajArray) {
		if (trajArray == null)
			return; // nothing to scramble
		int m = trajArray.length;
		int n = trajArray[0].length;
		Random rg = new Random();
		for (int k = 1; k < n; k++) {
			for (int i = 0; i < m; i++) {
				// swap trajArray[i][k] <-> trajArray[r][k] (with random r in
				// 0...m-1)
				int r = rg.nextInt(m);
				int[] tmp = trajArray[i][k];
				trajArray[i][k] = trajArray[r][k];
				trajArray[r][k] = tmp;
			}
		}
	}

}
